﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

[DirectMethodProxyID(IDMode = DirectMethodProxyIDMode.None)]
public partial class EShop_ViewSendReceiveMailDetail : System.Web.UI.Page
{
    UsersRepository URepo = new UsersRepository();
    ManageEmailMarketing MEM = new ManageEmailMarketing();
    static int CountDown = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int UserId = URepo.GetUserIdByName(_user["username"]);
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["ID"] != null)
            {
                try
                {
                    Guid Id =Guid.Parse(Request.QueryString["ID"]);
                    IEmailMarketings mails = MEM.GetEmailById(Id);
                    hdGuidId.Value = Request.QueryString["ID"];
                    if (mails != null)
                    {
                        lblSubject.Text = mails.EmailTitle;
                        lblFrom.Text = mails.UserEmail;
                        lblReceiveDate.Text = mails.EmailDate.Value.ToShortDateString();
                        ltrContent.Text = mails.EmailContent;
                        hdPointPlus.Value = mails.PointPlusUser.ToString();
                    }
                    else
                        Response.Redirect(ResolveUrl("~/Mailbox.html"));
                }
                catch
                {
                }
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            MEM.DeleteMail(Guid.Parse(hdGuidId.Value));
            
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
        finally
        {
            Response.Redirect(ResolveUrl("~/Eshop/ViewSendReceiveMail.aspx"));
        }
    }

    [DirectMethod]
    public static string ShowAlertDeleteMail()
    {
        return "Tin nhắn đã được xóa";
    }

    protected void GotoReplyPage(object sender, EventArgs e)
    {
        int MailId = int.Parse(Request.QueryString["ID"]);
        Response.Redirect(ResolveUrl("~/Tra-loi/" + MailId + ".html"));
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl("~/Eshop/ViewSendReceiveMail.aspx"));
    }
    void SetStatusWhenReadMail()
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        int UserId = URepo.GetUserIdByName(_user["username"]);
        Guid Id = Guid.Parse(hdGuidId.Value);
        MEM.SetMailAlreadyRead(Id);
        MEM.PlusPointUser(UserId, int.Parse(hdPointPlus.Value));
    }
    protected void UpdateTimer_Tick(object sender, EventArgs e)
    {
        DateStampLabel.Text = DateTime.Now.ToString();
        if (CountDown == 0)
        {
            UpdateTimer.Enabled = false;
            HttpCookie _user = Request.Cookies["userinfo"];
            int UserId = URepo.GetUserIdByName(_user["username"]);
            MEM.SetMailAlreadyRead(Guid.Parse(hdGuidId.Value));
            MEM.PlusPointUser(UserId, int.Parse(hdPointPlus.Value));
            
        }
        else
            CountDown--;
    }
}